<?php
/**
 *      [迷你云] (C)2009-2012 南京恒为网络科技.
 *   软件仅供研究与学习使用，如需商用，请访问www.miniyun.cn获得授权
 * 
 */
?>
<?php

class AdminUserPanelController extends MiniyunController
{
	public $layout='//layouts/admin';
	public function actionIndex()
	{
		$model= new UserListForm();
		if(Yii::app()->request->isPostRequest){
			$model->attributes=$_POST['UserListForm'];
			if($model->mutiAction()){				return;
			}
		}else{
			if(isset($_GET['UserListForm'])){
				$model->attributes=$_GET['UserListForm'];			}
		}
		$user = new User();
		$this->render('userList',array(
            'model'=>$model,
            'count'=>$user->count(),            'adminCount'=>$user->adminCount(),            'disabledCount'=>$user->disabledCount(),		));
	}
	
	public function actionDelete()
	{
		$model = new UserDeleteForm();
		$model->data = $model->search()->getData();
		if(Yii::app()->request->isPostRequest){
			$deleteCount = $model->deleteData();			$message = "删除  ".$deleteCount." 条用户成功";
			SystemManager::delUserSuccess();
			$this->redirect(Yii::app()->createUrl("adminUserPanel/index&message=".$message));
		}else{
			$this->render('userDelete',array(
             "data"=>$model->data
			));
		}
	}
	
	public function actionDisabled()
	{
		$model = new UserDisabledForm();
		$model->data = $model->search()->getData();
		if(Yii::app()->request->isPostRequest){
			$disabledCount = $model->disabledData();			$message = "禁用  ".$disabledCount." 条用户成功";
			$this->redirect(Yii::app()->createUrl("adminUserPanel/index&message=".$message));
		}else{
			$this->render('userDisabled',array(
             "data"=>$model->data
			));
		}
	}
	
	public function actionEnabled()
	{
		$model = new UserEnabledForm();
		$model->data = $model->search()->getData();
		if(Yii::app()->request->isPostRequest){
			$enabledCount = $model->enabledData();			$message = "解冻  ".$enabledCount." 条用户成功";
			$this->redirect(Yii::app()->createUrl("adminUserPanel/index&message=".$message));
		}else{
			$this->render('userEnabled',array(
             "data"=>$model->data
			));
		}
	}
	
	public function actionChangeRole()
	{
		$model = new ChangeRoleForm();
		$model->data = $model->search()->getData();
		$aim = Yii::app()->request->getParam("aim");
		if(Yii::app()->request->isPostRequest){
			if($aim=="admin"){
				$count = $model->changeRole(true);				$message = "改变  ".$count." 个用户为管理员";
				$this->redirect(Yii::app()->createUrl("adminUserPanel/index&message=".$message));
			}else{
				$count = $model->changeRole(false);				if($count==0)				{
					$message = "未选中有效记录";
				}else{
					$message = "改变  ".$count." 个用户为普通用户";
				}
				$this->redirect(Yii::app()->createUrl("adminUserPanel/index&message=".$message));
			}

		}else{
			$no = false;
			if($aim!="admin" && $model->isLastAdmin()){
								$no = true;
			}
			$this->render('changeRole',array(
             "data"=>$model->data,
            "no"=>$no,
			));
		}
	}
	
	public function actionStat(){
		$statForm = new UserStatForm();
		$statForm->model = new User();
		if(Yii::app()->request->isPostRequest){
			$statForm->startDate = $_POST['UserStatForm']["startDate"];
			$statForm->endDate   = $_POST['UserStatForm']["endDate"];
			$statForm->initData();
		}else{
			$statForm->initData();		}
		$this->render('stat',array(
             "data"=>$statForm,
		));
	}



	
	public function actionDeleteDevice()
	{
		if (!empty($_GET['id']) && !empty($_GET['action']))
		{
			$device = new UserDevice();
						$message = "删除设备成功";
			$this->redirect(Yii::app()->createUrl($this->id.'/'.$_GET['action'].'&message='.$message));
		}
	}

	
	public function actionCreate()
	{
		$model = new UserCreateForm();
		if(isset($_POST['UserCreateForm']))
		{
			$model->attributes=$_POST['UserCreateForm'];
			if($model->save()){
				$message = "新建用户成功";
				if (SYSTEM_STATUS == "init") {
				    SystemManager::addUserSuccess();
				}
				$this->redirect(Yii::app()->createUrl("adminUserPanel/index&message=".$message));
			}
		}
		$this->render('userCreate',array(
                    'model'=>$model
		));
	}

	
	public function actionUpdate()
	{
		if(empty($_REQUEST['id'])){
			$id=Yii::app()->session["user"]["id"];
		}else{
			$id=$_REQUEST['id'];
		}
		$model = new UserUpdateForm;
		$model->initData($id);
		if(Yii::app()->request->isPostRequest)
		{
			$model->attributes=$_POST['UserUpdateForm'];
			if($model->save()){
				$message = "修改用户成功";
				$this->redirect(Yii::app()->createUrl("adminUserPanel/index&message=".$message));
			}
		} 
		$this->render('userUpdate',array(
            'model'         => $model,
		));

	}
	
	public function actionShow()
	{
		if(empty($_REQUEST['id'])){
			$id=Yii::app()->session["user"]["id"];
		}else{
			$id=$_REQUEST['id'];
		}
		$model = new UserShowForm($id);
		if(Yii::app()->request->isPostRequest)
		{ 
			$model->attributes=$_POST['UserShowForm'];
		}
		$model->initStatData();
		$this->render('userShow',array(
            'model'         => $model,
		));

	}
}